<?php
class Representatives_model extends Model {

    function representatives_model() {
        parent::Model();
    }

    function addRepresentative($post) {
        $input = array(
            'rep_name'      => $post['firstname'],
            'rep_surname'   => $post['lastname'],
            'rep_email'     => $post['email'],
            'rep_status'    => '1',
            'role_id'       => $post['roles'],
            'mun_const_id'  => $post['municipalities']
        );
        $status = $this->db->insert('representatives', $input);
        return $status;
    }

    function updateRepresentative($post, $repID) {
        $input = array(
            'rep_name'      => $post['firstname'],
            'rep_surname'   => $post['lastname'],
            'rep_email'     => $post['email'],
            'rep_status'    => '1',
            'role_id'       => $post['roles'],
            'mun_const_id'  => $post['municipalities']
        );
        $this->db->where('rep_id', $repID);
        $status = $this->db->update('representatives', $input);
        return $status;
    }

    function getAllRepresentatives() {
        $representatives = $this->db->get('representatives');
        return $representatives->result();
    }
    
    function getRepresentativeById($rep_id) {
        $representative = $this->db->get_where('representatives', array('rep_id' => $rep_id));
        $result = $representative->result();
        if ($result > 0) {
            return $result[0];
        }
        else return false;
    }
    function getRepresentativeByIdRS($rep_id) {
        $representative = $this->db->get_where('representatives', array('rep_id' => $rep_id));
        $result = $representative->result();
        if ($result > 0) {
            return $result;
        }
        else return false;
    }

    //get all questions(and answers) for a specific representative
    function getRepAnswersForMessages($rep_id, $message_id = FALSE) {
        $this->db->select('*');
        $this->db->from('rep_messages');
        $this->db->join('messages', 'rep_messages.message_id = messages.message_id','left');
        $this->db->where('rep_messages.rep_id', $rep_id);
        if ($message_id) {
            $this->db->where('rep_messages.message_id', $message_id);
        }
        $query = $this->db->get();
        $result = $query->result();
        if ($result > 0) {
            return $result;
        }
        else return false;
    }

    //get the other representatives that have been asked the same questions as a specific representative
    function getOtherRepMessages($rep_id) {
        $query = $this->db->query(
                'select
                    *
                from rep_messages as r
                left join representatives as l on r.rep_id = l.rep_id
                where r.message_id in
                    (select message_id
                    from rep_messages
                    where rep_id ='.$rep_id.')
                and r.rep_id != '.$rep_id
                );
        $result = $query->result();
        if ($result > 0) {
            return $result;
        }
        else return false;
    }
    
    //zemi gi site pratenici
    function getAllPratenici($lang = 1, $munId = false){
        $this->db->select('*');
        $this->db->from('representatives');
        $this->db->where(array('rep_status' => '1'));
        if ($munId) {
            $this->db->where(array('mun_const_id' => $munId,'representatives.role_id'=>1));
        }
        else {
            $this->db->where(array('representatives.role_id'=>1));
        }
        $this->db->join('municipalities','mun_const_id = municipality_id', 'left');
        $this->db->where(array('municipality_lang' => $lang));
        $this->db->join('lopovi_roles','representatives.role_id = lopovi_roles.role_id', 'left');
        $this->db->where(array('role_lang' => $lang));
        $this->db->order_by('rep_name','asc');
        $query = $this->db->get();
        return $query->result();
    }
    
    //Zemi gi site sovetnici za konkretna opstina
    function getAllSovetnici($lang = 1, $munId = false){
        $this->db->select('*');
        $this->db->from('representatives');
        $this->db->where(array('rep_status' => '1'));
        if ($munId) {
            $this->db->where(array('mun_const_id' => $munId,'representatives.role_id'=>2));
        }
        else {
            $this->db->where(array('representatives.role_id'=>2));
        }
        $this->db->join('municipalities','mun_const_id = municipality_id', 'left');
        $this->db->where(array('municipality_lang' => $lang));
        $this->db->join('lopovi_roles','representatives.role_id = lopovi_roles.role_id', 'left');
        $this->db->where(array('role_lang' => $lang));
        $this->db->order_by('rep_name','asc');
        $query = $this->db->get();
        return $query->result();
    }

    //Zemi gi site gradonacalnici
    function getAllGradonacalnici($lang = 1, $munId = false){
        $this->db->select('*');
        $this->db->from('representatives');
        $this->db->where(array('rep_status' => '1'));
        if ($munId) {
            $this->db->where(array('mun_const_id' => $munId, 'representatives.role_id'=>3));
        }
        else {
            $this->db->where(array('representatives.role_id'=>3));
        }
        $this->db->join('municipalities','mun_const_id = municipality_id', 'left');
        $this->db->where(array('municipality_lang' => $lang));
        $this->db->join('lopovi_roles','representatives.role_id = lopovi_roles.role_id', 'left');
        $this->db->where(array('role_lang' => $lang));
        $this->db->order_by('rep_name','asc');
        $query = $this->db->get();
        return $query->result();
    }

    //Zemi gi site gradonacalnici+sovetnici+pratenici
    function getAll($lang = 1, $munId = false){
        $this->db->select('*');
        $this->db->from('representatives');
        $this->db->where(array('rep_status' => '1'));
        if ($munId) $this->db->where(array('mun_const_id' => $munId));
        $this->db->join('municipalities','mun_const_id = municipality_id', 'left');
        $this->db->where(array('municipality_lang' => $lang));
        $this->db->join('lopovi_roles','representatives.role_id = lopovi_roles.role_id', 'left');
        $this->db->where(array('role_lang' => $lang));
        $this->db->order_by('rep_name','asc');
        $query = $this->db->get();
//        print_r($this->db->queries);
        return $query->result();
    }

    function insertRepMessage($data){
        $this->db->insert('rep_messages',$data);
    }

    function deleteRepresentative($repID) {
        $data = array(
               'rep_status' => '2'
            );
        $this->db->where('rep_id', $repID);
        $result = $this->db->update('representatives', $data);
        return $result;
    }


}
?>
